import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import storage from './storage';
import OpenNotification from '@/components/Notification';
import store from '@/store/store';


// const walletAddress = store.getState().walletAddress
const address: any = storage.localGet('address')
const lang: any = storage.localGet('lang')

const service = axios.create({
  // baseURL: 'http://localhost:3000',
  baseURL: '',
  timeout: 10000,
  headers: {
    // Accept: '*/*',
    Accept: 'application/json',
    // 'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
    'Accept-Language': lang,
    // address: '0x9069383167162ea7DE5d3cD9742628A75B6fabCB',
    address: address || '',
  },
});

// service.interceptors.request.use(
//   // 统一请求前处理
//   (config: AxiosRequestConfig | any) => {
//     const token = storage.localGet('token');
//     if (token) {
//       config.headers.common['Authorization'] = 'Bearer ' + token;
//     }
//     return config;
//   },
//   (error: any) => {
//     return Promise.reject(new Error(error));
//   }
// );

// service.interceptors.response.use(
//   // 统一返回数据处理
//   (response: AxiosResponse) => {
//     const code = response.data?.code;
//     // token过期
//     if (code === 401 || code === 1003) {
//       OpenNotification({
//         type: 'warning',
//         message: 'token overdue!',
//       });
//       storage.localRemove('sign');
//       storage.localRemove('token');
//       window.location.reload();
//     }
//     return response.data;
//   },
//   (error: any) => {
//     // return Promise.reject(new Error(error));
//   }
// );

const request: any = async (
  method: any,
  url: any,
  params = {
    query: undefined,
    body: undefined,
  },
  filter = true
) => {
  const { query, body, ...otherParams } = params;
  const response: any = await service({
    url,
    method,
    params: query,
    data: body,
    ...otherParams,
  });
  if (!response) return false;
  if (!filter) return response;
  // if (response && response?.data.code !== 0) {
  //   console.log('response', response);

  //   response?.data?.msg &&
  //     OpenNotification({
  //       type: 'error',
  //       message: response.data?.msg
  //     })
  //   return false;
  // }
  return filter ? response?.data || {} : response || {};
};

export default request;
